#include "compile_run.hpp"
#include "../comm/httplib.h"
#include <string>
#include <stdlib.h>
using namespace ns_compile_and_run;
using namespace httplib;

//./compile_server port

void Usage(const char *name)
{
   std::cerr << "Usage: " << name << " port" << std::endl;
}
int main(int argc, char *argv[])
{
    if(argc < 2) {
        Usage(argv[0]);
        return 1;
    }
    Server svr;
    //这段代码实现了一个 HTTP GET 接口 /hello，用于返回一段简单的文本内容,用于测试服务区是否正常运行
    svr.Get("/hello",[](const Request &req, Response &resp) {
        resp.set_content("hello httplib!", "text/plain;charset=utf-8");
    });
    //这段代码实现了一个 HTTP POST 接口 /comile_and_run，用于接收客户端提交的代码编译和运行请求
    svr.Post("/compile_and_run",[](const Request &req, Response &resp) {
       //用户请求的服务正文是我们想要的json string
       std::string in_json = req.body;
       std::string out_json;
       if(!in_json.empty()) {
           CompileAndRun::Start(in_json,&out_json);
           resp.set_content(out_json,"application/json;charset=utf-8");
       } 
    });
    //svr.set_base_dir("./wwwroot");
    svr.listen("0.0.0.0",atoi(argv[1])); // 启动http服务，监听所有IP的8080端口
    return 0;
}
